﻿using System.Security.Cryptography;

namespace OneCard.Face8280Module.Utility
{
    public static class MD5Helper
    {
        public static string GetStreamMD5ByHex(Stream bufStream)
        {
            MD5 md5 = MD5.Create();

            bufStream.Position = 0;
            int iLen = (int)bufStream.Length;


            //AddLog($"文件长度： {iLen}");
            byte[] md5Value = md5.ComputeHash(bufStream);

            return Convert.ToHexString(md5Value);


        }
        public static string GetByteBufMD5(byte[] buf)
        {
            MD5 md5 = MD5.Create();
            //FileStream  

            using (MemoryStream sRead = new MemoryStream(buf))
            {
                sRead.Position = 0;
                int iLen = (int)sRead.Length;


                //AddLog($"文件长度： {iLen}");
                byte[] md5Value = md5.ComputeHash(sRead);

                return Convert.ToBase64String(md5Value);
            }

        }


        public static string GetFileMD5(string sFile)
        {
            MD5 md5 = MD5.Create();
            //FileStream  

            using (FileStream sRead = new FileStream(sFile, FileMode.Open, FileAccess.Read))
            {
                int iLen = (int)sRead.Length;

                //AddLog($"文件长度： {iLen}");
                byte[] md5Value = md5.ComputeHash(sRead);

                return Convert.ToBase64String(md5Value);
            }

        }


        public static string GetStringMD5(string sText)
        {
            MD5 md5 = MD5.Create();
            //FileStream  
            Encoding encoding = Encoding.UTF8;

            using (MemoryStream sRead = new MemoryStream(encoding.GetBytes(sText)))
            {
                int iLen = (int)sRead.Length;

                //AddLog($"文件长度： {iLen}");
                byte[] md5Value = md5.ComputeHash(sRead);

                return Convert.ToBase64String(md5Value);
            }

        }

    }
}
